import json
from pprint import pprint

import requests
from fontTools.ttLib import TTFont
from lxml import etree


def save_font_file():
    headers = {
        "accept": "*/*",
        "accept-encoding": "gzip, deflate, br",
        "accept-language": "zh-CN,zh;q=0.9",
        "origin": "https://www.guazi.com",
        "referer": "https://www.guazi.com/",
        "sec-ch-ua": "\".Not/A)Brand\";v=\"99\", \"Google Chrome\";v=\"103\", \"Chromium\";v=\"103\"",
        "sec-ch-ua-mobile": "?0",
        "sec-ch-ua-platform": "\"Windows\"",
        "sec-fetch-dest": "font",
        "sec-fetch-mode": "cors",
        "sec-fetch-site": "same-site",
        "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36"
    }
    req = requests.get("https://oss.guazi.com/gzim/bd208ce8-20ff-4200-ba8c-58ca7b70fd38.woff2", headers=headers)
    with open("guazi.woff2", "wb") as f:
        f.write(req.content)


def uni_num_dict() -> dict:
    """ 获取编码对应的数字 """
    font_map = {
        'uniE9CE': 0,
        'uniE41D': 1,
        'uniE630': 2,
        'uniEAF2': 3,
        'uniE325': 4,
        'uniE891': 5,
        'uniEC4C': 6,
        'uniE1D0': 7,
        'uniE76E': 8,
        'uniE52E': 9
    }
    font = TTFont("./guazi.woff2")
    font.saveXML(r"font.xml")
    code_name_map = font.getBestCmap()
    print(code_name_map)
    new_map = {f"&#{num};": font_map[code] if code in font_map else code for num, code in dict(code_name_map).items()}
    print("new_map:", new_map)
    return new_map


def get_data():
    headers = {
        "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
        "accept-encoding": "gzip, deflate, br",
        "accept-language": "zh-CN,zh;q=0.9",
        "cache-control": "max-age=0",
        "cookie": "platform=pc; cityId=12; cityDomain=bj; cityName=%E5%8C%97%E4%BA%AC; gzSupportWebp=1; uuid=a815462c-3d31-4572-9c52-5facd0aa7ff8; sessionid=47f9a0d4-cfc8-47b2-fc51-1bc57e1f8268; cainfo=%7B%22ca_s%22%3A%22self%22%2C%22ca_n%22%3A%22self%22%2C%22ca_medium%22%3A%22-%22%2C%22ca_term%22%3A%22-%22%2C%22ca_content%22%3A%22-%22%2C%22ca_campaign%22%3A%22-%22%2C%22ca_kw%22%3A%22-%22%2C%22ca_i%22%3A%22-%22%2C%22scode%22%3A%22-%22%2C%22guid%22%3A%22a815462c-3d31-4572-9c52-5facd0aa7ff8%22%7D; ca_s=self; ca_n=self; DATE=1657109582531; crystal=U2FsdGVkX19rbvdayY1vPzuf6WrB9JisBrAhtch3pCx+D/4CQHc973dDW0Rb76IZjKy7eQNv3XasR7/8PRYrMeRpBUFCWQTKYW/n9wVPQzmbJy2nJyI1f9k8IGtDRf54Xwk1FZRKrWpX5O5MMNYHzeINEwuEVIL+d26frQm0E0Xgfuv9Jkorv6HqI4m+lpEtvim6oL3p7+qyA1zx5uiaWc0tPIM1A310u0k1CfLjZ5kc0RxXPQIC+RzUD/I9OhgS",
        "if-none-match": "\"11128c-D4m6ZWKS9ipj2Tl/CAIgxj+NrgM\"",
        "sec-ch-ua": "\".Not/A)Brand\";v=\"99\", \"Google Chrome\";v=\"103\", \"Chromium\";v=\"103\"",
        "sec-ch-ua-mobile": "?0",
        "sec-ch-ua-platform": "\"Windows\"",
        "sec-fetch-dest": "document",
        "sec-fetch-mode": "navigate",
        "sec-fetch-site": "none",
        "sec-fetch-user": "?1",
        "upgrade-insecure-requests": "1",
        "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36"
    }
    # gz_url = "https://mapi.guazi.com/car-source/carList/pcList?osv=Unknown&minor=&sourceType=&ec_buy_car_list_ab=&location_city=&district_id=&tag=-1&license_date=&auto_type=&driving_type=&gearbox=&road_haul=&air_displacement=&emission=&car_color=&guobie=&bright_spot_config=&seat=&fuel_type=&order=&priceRange=0,-1&tag_types=&diff_city=&intention_options=&initialPriceRange=&monthlyPriceRange=&transfer_num=&car_year=&carid_qigangshu=&carid_jinqixingshi=&cheliangjibie=&page=1&pageSize=20&city_filter=12&city=12&guazi_city=12&qpres=&platfromSource=wap&versionId=0.0.0.0&sourceFrom=wap&deviceId=a815462c-3d31-4572-9c52-5facd0aa7ff8"
    gz_url = 'https://mapi.guazi.com/car-source/carList/pcList?versionId=0.0.0.0&sourceFrom=wap&deviceId=a815462c-3d31-4572-9c52-5facd0aa7ff8&osv=Windows+10&minor=&sourceType=&ec_buy_car_list_ab=&location_city=&district_id=&tag=-1&license_date=&auto_type=&driving_type=&gearbox=&road_haul=&air_displacement=&emission=&car_color=&guobie=&bright_spot_config=&seat=&fuel_type=&order=&priceRange=0,-1&tag_types=&diff_city=&intention_options=&initialPriceRange=&monthlyPriceRange=&transfer_num=&car_year=&carid_qigangshu=&carid_jinqixingshi=&cheliangjibie=&page=2&pageSize=20&city_filter=16&city=16&guazi_city=16&qpres=568978945492897792&platfromSource=wap'
    res = requests.get(gz_url, headers=headers)
    text = res.text
    for uni_K, num in uni_num_dict().items():
        text = text.replace(uni_K, str(num))
    gz_data = json.loads(text)
    car_list = gz_data.get("data").get("postList")
    for car in car_list:
        car_data = {
            "title": car["title"],
            "road_haul": car["road_haul"],
            "first_pay": car["first_pay"],
            "price": car["price"]
        }
        print(car_data)


get_data()
# save_font_file()



